﻿using System;
using System.Collections.Generic;
using System.IO;
using BulkUploadGovernanceFiles.Models;
using BulkUploadGovernanceFiles.MongoSpecification;
using BulkUploadGovernanceFiles.Repositories;

namespace BulkUploadGovernanceFiles.SyncroniseInsights
{
    public class ProcessGovernanceCompanyProfile
    {
        private readonly GovernanceCompanyProfileRepository _governanceCompanyProfileRepository;
        private readonly GovernanceCompanyProfile _governanceCompanyProfile;

        public GovernanceCompanyProfile GovernanceCompanyProfile
        {
            get { return _governanceCompanyProfile; }
        }

        public ProcessGovernanceCompanyProfile(Guid governanceProfileCompanyId)
        {
            _governanceCompanyProfileRepository = new GovernanceCompanyProfileRepository();
            GovernanceCompanyProfileHaveCompanyId specHaveCompanyId = new GovernanceCompanyProfileHaveCompanyId(governanceProfileCompanyId);
            _governanceCompanyProfile = _governanceCompanyProfileRepository.Get(specHaveCompanyId);
        }

        public void UpdateAnalystViewAndInsights(GovernanceProfile profile)
        {
            _governanceCompanyProfile.AnalystView = profile.Description;
             
            foreach (var alert in profile.Alerts)
            {
                if (_governanceCompanyProfile.Insights == null)
                {
                    _governanceCompanyProfile.Insights = new List<GovernanceInsight>();
                }
                GovernanceInsight governanceInsight = new GovernanceInsight();
                governanceInsight.Id = alert.Id;
                governanceInsight.CreatedBy = alert.CreatedBy;
                governanceInsight.CreatedDate = alert.CreatedDate;
                governanceInsight.FileId = alert.FileId;
                governanceInsight.PublicationDate = alert.PublicationDate;
                governanceInsight.Title = alert.Title;
                _governanceCompanyProfile.Insights.Add(governanceInsight);
            }

            _governanceCompanyProfileRepository.Update(_governanceCompanyProfile);
        }

        public void CreateInsightFileMetadata(GovernanceInsightInputFile file)
        {
            GovernanceInsightInputFileRepository governanceInsightInputFileRepository = new GovernanceInsightInputFileRepository();
           // ISpecification<GovernanceInsightInputFile> specification = new GovernanceInsightInputFileHaveCompanyId(file.CompanyId);
           // var existingFile = governanceInsightInputFileRepository.GetByCompanyId(specification);
            governanceInsightInputFileRepository.InsertNew(file);
        }

        public void SavePdfFileToMongo(Guid fileId, string mongoFileName, Stream fileStream)
        {
            string connectionString = ConfigManager.GovernanceInputFile.ConnectionString;
            string dataBaseName = ConfigManager.GovernanceInputFile.DataBaseName;
            
            MongoGridFSRepository _insightFileRepository = new MongoGridFSRepository(dataBaseName, connectionString);
            //string fileName = _insightFileRepository.ReadFileName(fileId);
            //if (fileName != null)
            //    _insightFileRepository.DeleteFile(fileId);
            const string contentType = "application/pdf";
            var mongoData = _insightFileRepository.NewFile(mongoFileName, fileId, contentType);

            using (mongoData.Item1)
            {
                fileStream.CopyTo(mongoData.Item1);
            }
        }
    }
}